package com.dmj.sqldsl.builder;

import com.dmj.sqldsl.builder.column.ColumnBuilder;
import com.dmj.sqldsl.builder.config.EntityConfig;
import com.dmj.sqldsl.builder.config.GlobalConfig;
import com.dmj.sqldsl.model.DslUpdate;
import java.util.List;
import java.util.stream.Collectors;

public class UpdateFinishBuilder<T, R> {

  private final UpdateWhereBuilder<T> updateWhereBuilder;

  private final List<ColumnBuilder<T, R>> returningColumnBuilders;

  public UpdateFinishBuilder(UpdateWhereBuilder<T> tUpdateBuilder,
      List<ColumnBuilder<T, R>> returningColumnBuilders) {
    this.updateWhereBuilder = tUpdateBuilder;
    this.returningColumnBuilders = returningColumnBuilders;
  }

  public DslUpdate toUpdate() {
    EntityConfig entityConfig = GlobalConfig.getEntityConfig();
    DslUpdate update = updateWhereBuilder.toUpdate(entityConfig);
    update.setReturningColumns(returningColumnBuilders.stream().map(x -> x.build(entityConfig))
        .collect(Collectors.toList()));
    return update;
  }
}
